/*
	Sleepless Team
	Created By: Daifu Ye, Faith Or, Lam Lu, Phuc Nguyen
*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <crtdbg.h> //check memory leak

#include "model.h"
#include "controller_manDaifu.h"
#include "UserIO.h"

#define FLUSH while( getchar() != '\n' )
#define MAX_STR 100
#define HASH_SIZE 81 //to reduce the number of collisions

void animation();
HWND BCX_Bitmap(char*, HWND, int, int, int, int, int, int, int, int);
HWND GetConsoleHwnd(void);

int main (void)
{
	//Local Declarations
	BST *bst;
	HEAD_NODE *headNode;
	HASH *hash;
	STACK *stack;
	char option;

	// Statements
	SetConsoleTitle("Sleepless Database Application");
	option = getOption();
	if(option == 'Y')
		animation();
	buildManager(&hash, &bst, &stack, HASH_SIZE);
	headNode = iniHeadNode();
	createHeadNode(headNode, hash, bst, stack);
	processManager(headNode, HASH_SIZE);
	memoryManager(headNode);
	fare();
	
	printf( _CrtDumpMemoryLeaks() ? "\tError: There's a leak in memory!!\n" : "\tThere are no memory leaks. :-D\n");
	
	system("pause");
	return 0;
}// main

void animation() {
	//Local Definitions
	int id;
	HWND hConWnd;
	HWND B;
	char usArmyAry[5][MAX_STR] = {"usArmy.bmp", "usNavy.bmp", "usAirForce.bmp", "usNavy.bmp", "usCoastGuard.bmp"};
	char sleepless[7][MAX_STR] = {"animation0.bmp", "animation1.bmp", "animation2.bmp", "animation5.bmp", "animation3.bmp", "animation5.bmp", "animation8.bmp"};
	int i;
	int armyAryCount = 5;
	int sleepAryCount = 7;

	// Statements
	hConWnd = GetConsoleHwnd();
	if (hConWnd)
	{
		// select a bitmap file you have or use one of the files in the Windows folder
		// filename-exact location for the image, handle, ID, ulcX, ulcY, width, height   0,0 auto-adjusts
		srand(time(NULL));
		for(i = 0; i < armyAryCount; i++) {
			id = rand() % 1000; //create a radom id for child window
			B = BCX_Bitmap(usArmyAry[i], hConWnd, id, 1, 1, 0, 0, 0, 0, 0);
			Sleep(2000);
			DestroyWindow(B);
		}
		
		for(i = 0; i < sleepAryCount; i++) {
			id = rand() % 1000; //create a radom id for child window
			B = BCX_Bitmap(sleepless[i], hConWnd, id, 1, 1, 0, 0, 0, 0, 0);
			if(i == 5)
				Sleep(700);
			else if(i == 6)
				Sleep(7000);
			else
				Sleep(500);
			DestroyWindow(B);
		}
	}
	
	hConWnd = NULL;
	return;
}

// draw the bitmap
HWND BCX_Bitmap(char* Text,HWND hWnd,int id,int X,int Y,int W,int H,int Res,int Style,int Exstyle)
{
	HWND A;
	HBITMAP hBitmap;
	int success = 1;

// set default style
	if (!Style) Style = WS_CLIPSIBLINGS|WS_CHILD|WS_VISIBLE|SS_BITMAP|WS_TABSTOP;

// form for the image
	A = CreateWindowEx(Exstyle,"Static", "Sleepless", Style, W, H, 0, 0, hWnd, (HMENU)id, GetModuleHandle(0), NULL);
	if (A == NULL) {
		printf("Error in Creating Window\n");
		success = 0;
		return NULL;
	}

// Text contains filename
	hBitmap = (HBITMAP) LoadImage(NULL, Text, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);

// auto-adjust width and height
	SendMessage(A, (UINT)STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap);
	UpdateWindow(A);
	return A;
}

HWND GetConsoleHwnd(void)
{
	#define MY_BUFSIZE 1024 // Buffer size for console window titles.
	HWND hwndFound;         // This is what is returned to the caller.
	char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
										// WindowTitle.
	char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
										// WindowTitle.

	// Fetch current window title.

	GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);

	// Format a "unique" NewWindowTitle.

	wsprintf(pszNewWindowTitle,"%d/%d",
		GetTickCount(),
		GetCurrentProcessId());

	// Change current window title.

	SetConsoleTitle(pszNewWindowTitle);

	// Ensure window title has been updated.

	Sleep(500);

	// Look for NewWindowTitle.

	hwndFound = FindWindow(NULL, pszNewWindowTitle);

	// Restore original window title.

	SetConsoleTitle(pszOldWindowTitle);

	return(hwndFound);
}
